// mircoApp/store-oil-station/refueling_list/refueling_list.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    storeList: [],
    storeId: "",
    point: {},
    city: "",

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.store_id) {
      this.setData({
        storeId: options.store_id
      })
    }
    this.init();
    this.getData();
  },
  tapStoreLis(e) {
    console.log(e)
    let {
      id
    } = e.currentTarget.dataset
    this.setData({
      storeId: id
    })
    wx.redirectTo({
      url: '/mircoApp/store-oil-station/refueling_payment/refueling_payment?store_id=' + this.data.storeId,
    })
  },
  async getData() {
    let json = {}
    await this._getLocation((res) => {
      json.city = res.address_component.city;
      json.lng = res.location.lng;
      json.lat = res.location.lat;
      this.data.point = {
        lng: res.location.lng,
        lat: res.location.lat,
        city: res.address_component.city
      }
      this.setData({
        full_address: res.address,
        page: 1,
        total_page: 1
      })
      this.firstGetStore();
    }, (e) => {
      wx.showModal({
        title: '定位失败',
        content: '请确认授权定位是否开启',
        confirmText: '手动定位',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=oil',
            })
          }
        }
      })
    })
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        lng: this.data.point.lng,
        lat: this.data.point.lat,
        city: this.data.city,
        name: this.data.storeNameKey,
        page: this.data.page
      };
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.store-oil-station.frontend.unpaid-order.store"),
        data: json
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          storeList: this.data.storeList.concat(data.data.data)
        })
      } else {
        app.tips(data.msg);
        this.data.page = this.data.page - 1;
      }
    }
  },
  async firstGetStore() {
    let json = {
      lng: this.data.point.lng,
      lat: this.data.point.lat,
      city: this.data.city,
      name: this.data.storeNameKey
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-oil-station.frontend.unpaid-order.store"),
      data: json
    })
    if (data.result) {
      let store = data.data.data;
      this.setData({
        storeList: store,
        total_page: data.data.last_page,
        isLoadMore: true,
      })
      if (!this.options.store_id) {
        let cind;
        for (let i = 0; i < store.length; i++) {
          if (store[i].is_default == 1) {
            cind = i;
            break;
          }
        }
        if (cind) {
          this.setData({
            storeId: store[cind].id
          })
        } else {
          this.setData({
            storeId: store[0].id
          })
        }
      }
    } else {
      app.tips(data.msg);
    }
  },
  init() {
    this.setData({
      page: 1,
      total_page: 1,
      isLoadMore: true,
      storeList: []
    })
  },
  goToAdress(e) {
    let {
      item
    } = e.currentTarget.dataset;
    let latitude = item.latitude;
    let longitude = item.longitude;
    let name = item.store_name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})